package org.ssp.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.ssp.base.ResultSetHanlder;
import org.ssp.dao.AssistDao;
import org.ssp.pojo.Assister;
import org.ssp.util.DBUtil;

public class AssistImpl implements AssistDao {
    //增加assist
	@Override
	public void addAssister(Assister assist) {
		String sql="insert into assist(id,name,password,type) values(?,?,?,1)";
		DBUtil.update(sql, assist.getId(),assist.getName(),assist.getPassword());
	}
    //删除assist
	@Override
	public void deleteAssister(String id) {
		String sql="delete from assist where id=?";
		DBUtil.update(sql, id);
	}
    //更新
	@Override
	public void updateAssister(Assister assist) {
		String sql="update assist set password=? where id=?";
		DBUtil.update(sql,assist.getPassword(),assist.getId());
	}
	@Override
	public void updateAssister2(Assister assist) {
		String sql="update assist set name=?,department=?,phone=?,email=?,wxid=?,wxcode=? where id=?";
		DBUtil.update(sql,assist.getName(),assist.getDepartment(),assist.getPhone(),assist.getEmail(),assist.getWxid(),assist.getWxcode(),assist.getId());
	}
	@Override
	public ArrayList<Assister> findAll(int offset,int rows) {
		String sql="select * from assist where type=1 limit ?,?";
		return (ArrayList<Assister>)DBUtil.query(sql, new ResultSetHanlder(){

			@Override
			public Object doHandler(ResultSet rs) {
				ArrayList<Assister> list=new ArrayList<Assister>();
				try {
					while(rs.next()){
						Assister assist=new Assister();
						assist.setId(rs.getString("id"));
						assist.setName(rs.getString("name"));
						assist.setDepartment(rs.getString("department"));
						assist.setPhone(rs.getString("phone"));
						assist.setEmail(rs.getString("email"));
						assist.setWxid(rs.getString("wxid"));
						assist.setWxcode(rs.getString("wxcode"));
						assist.setPassword(rs.getString("password"));
						assist.setType(rs.getInt("type"));
						list.add(assist);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					return null;
				}
				return list;
			}
			
		}, offset,rows);
	}
	@Override
	public int findAll() {
		String sql="select *  from assist where type=1";
		return (Integer)DBUtil.query(sql, new ResultSetHanlder(){

			@Override
			public Object doHandler(ResultSet rs) {
				Integer result=0;
				try {
					while(rs.next()){
						result+=1;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					return null;
				}
				return result;
			}
		});
	}
	@Override
	public Assister findById(String id) {
		String sql="select * from assist where id=?";
		return (Assister)DBUtil.query(sql, new ResultSetHanlder(){
			@Override
			public Object doHandler(ResultSet rs) {
				Assister assist=null;
				try {
					if(rs.next()){
						assist=new Assister();
						assist.setId(rs.getString(1));
						assist.setName(rs.getString(2));
						assist.setDepartment(rs.getString(3));
						assist.setPhone(rs.getString(4));
						assist.setEmail(rs.getString(5));
						assist.setWxid(rs.getString(6));
						assist.setWxcode(rs.getString(7));
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					return null;
				}
				return assist;
			}
			
		}, id);
		
	}
	//管理员登录验证
	@Override
	public Assister findAssist(String id,String pwd) {
		String sql="select * from assist where id=? and password=?;";
		System.out.println("构造的sql"+sql);
		return (Assister)DBUtil.query(sql, new ResultSetHanlder(){

			@Override
			public Object doHandler(ResultSet rs) {
				Assister assist=null;
				try {
					if(rs.next()){
					 assist=new Assister();
					 assist.setDepartment(rs.getString("department"));
					 assist.setEmail(rs.getString("email"));
					 assist.setId(rs.getString("id"));
					 assist.setName(rs.getString("name"));
					 assist.setPassword(rs.getString("password"));
					 assist.setPhone(rs.getString("phone"));
					 assist.setType(rs.getInt("type"));
					 assist.setWxcode(rs.getString("wxid"));
					 assist.setWxcode(rs.getString("wxcode"));
					 }
				} catch (SQLException e) {
					e.printStackTrace();
					return null;
				}
				return assist;
			}
			
		}, id,pwd);
	}
	@Override
	public Assister getAssistByUid(String uid) {
		String sql="select assist.name,assist.phone,assist.email,assist.wxid,assist.wxcode from assist inner join user on assist.id=user.assistid and user.uid=?;";
		return (Assister)DBUtil.query(sql, new ResultSetHanlder() {
			
			@Override
			public Object doHandler(ResultSet rs) {
				Assister assist=null;
				try {
					if(rs.next()){
						assist=new Assister();
						assist.setName(rs.getString("name"));
						assist.setEmail(rs.getString("email"));
						assist.setPhone(rs.getString("phone"));
						assist.setWxcode(rs.getString("wxcode"));
						assist.setWxid(rs.getString("wxid"));
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return assist;
			}
		}, uid);
	}

}
